import { getAttribLocation } from './gl/gl-loc';
import { VSHADER_SOURCE, FSHADER_SOURCE } from './gl/gl-glsl'
import { initShaders } from './gl/gl-shader';
export const glLocation = {
  u_ModelViewMatrix: undefined,
  u_ProjMatrix: undefined,
  // u_ModelMatrix: undefined,
  a_Position: undefined,
  a_PointSize: undefined,
  u_FragColor: undefined,
}
export const ctx = {
  canvas: null,
  gl: null
};
export class WebGLRenderer {
  constructor(params = {}) {
    console.log('constructor');
    ctx.canvas = document.createElement('canvas');
    if (!ctx.canvas) {
      console.log('Failed to retrieve the <canvas> element');
      return false; 
    }
    ctx.canvas.width = window.innerWidth;
    ctx.canvas.height = window.innerHeight - 10;
  }
  setSize(width, height){
    console.log('setSize');
    ctx.canvas.width = width;
    ctx.canvas.height = height - 200;
  }
  setClearColor(){

  }
  render(params = {}) {
    console.log('render');
    const { id } = params;
    const container = document.getElementById(id);
    console.log('ctx.gl', ctx.gl);
    if(ctx.gl) return;
    container.appendChild(ctx.canvas);
    ctx.gl = ctx.canvas.getContext('webgl')
    if (!initShaders(ctx.gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
      console.log('Failed to intialize shaders.');
      return;
    }
    glLocation.a_Position = getAttribLocation(ctx.gl, 'a_Position');
    glLocation.a_PointSize = getAttribLocation(ctx.gl, 'a_PointSize');
    ctx.gl.clearColor(0.0, 0.0, 0.0, 1.0);
    ctx.gl.clear(ctx.gl.COLOR_BUFFER_BIT);
    // glLocation.a_PointSize = glLocation?.a_PointSize ?? getAttribLocation(this._gl, 'a_PointSize')
    // glLocation.a_Position = glLocation?.a_Position ?? getAttribLocation(this._gl, 'a_Position')
    // glLocation.u_FragColor = glLocation?.u_FragColor ?? getUniformLocation(this._gl, 'u_FragColor')
    // glLocation.u_ModelViewMatrix = glLocation?.u_ModelViewMatrix ?? getUniformLocation(this._gl, 'u_ModelViewMatrix')
    // glLocation.u_ProjMatrix = glLocation?.u_ProjMatrix ?? getUniformLocation(this._gl, 'u_ProjMatrix')
  }
}

